//选择时间
export const formatDate = (date) => {
  date = new Date(date);
  return `${date.getMonth() + 1}/${date.getDate()}`;
}

/**
 * date 为日期Date类型，
 * 日期格式化信息 matter 定义 
 * 年:yyyy/YYYY/yy/YY
 * 月:mm/MM (不足两位用0补全)
 * 日:dd/DD (不足两位用0补全)
 * 时:hh/HH (24小时制)
 * 分:mi/MI (不足两位用0补全)
 * 秒:ss/SS (不足两位用0补全)
 */
export const format = (date, matter) => {
  let year = date.getFullYear().toString();
  let month = (date.getMonth() + 1).toString();
  month = (month.length > 1) ? month : ('0' + month);
  let day = date.getDate().toString();
  day = (day.length > 1) ? day : ('0' + day);
  let hours = date.getHours().toString();
  hours = (hours.length > 1) ? hours : ('0' + hours);
  let minutes = date.getMinutes().toString();
  minutes = (minutes.length > 1) ? minutes : ('0' + minutes);
  let seconds = date.getSeconds().toString();
  seconds = (seconds.length > 1) ? seconds : ('0' + seconds);
  let retVal = matter;
  if (matter.indexOf('yyyy') >= 0) {
    retVal = retVal.replace('yyyy', year);
  } else if (matter.indexOf('YYYY') >= 0) {
    retVal = retVal.replace('YYYY', year);
  } else if (matter.indexOf('yy') >= 0) {
    retVal = retVal.replace('yy', year.substring(2));
  } else if (matter.indexOf('YY') >= 0) {
    retVal = retVal.replace('YY', year.substring(2));
  }
  if (matter.indexOf('mm') > 0) {
    retVal = retVal.replace('mm', month);
  } else if (matter.indexOf('MM') > 0) {
    retVal = retVal.replace('MM', month);
  }

  if (matter.indexOf('dd') > 0) {
    retVal = retVal.replace('dd', day);
  } else if (matter.indexOf('DD') > 0) {
    retVal = retVal.replace('DD', day);
  }


  if (matter.indexOf('hh') > 0) {
    retVal = retVal.replace('hh', hours);
  } else if (matter.indexOf('HH') > 0) {
    retVal = retVal.replace('HH', hours);
  }


  if (matter.indexOf('mi') > 0) {
    retVal = retVal.replace('mi', minutes);
  } else if (matter.indexOf('MI') > 0) {
    retVal = retVal.replace('MI', minutes);
  }


  if (matter.indexOf('ss') > 0) {
    retVal = retVal.replace('ss', seconds);
  } else if (matter.indexOf('SS') > 0) {
    retVal = retVal.replace('SS', seconds);
  }
  return retVal;
}

/**
 * 添加提示音
 */
export const payAudio = () => {
  const audioContext = wx.createInnerAudioContext();
  audioContext.src = '/audio/message_red.mp3';
  audioContext.play();
  audioContext.onEnded(() => {
    console.log('播放结束');
  });
}

export const tansParams = (params) =>{
  let result = ''
  for (const propName of Object.keys(params)) {
    const value = params[propName];
    var part = encodeURIComponent(propName) + "=";
    if (value !== null && value !== "" && typeof (value) !== "undefined") {
      if (typeof value === 'object') {
        for (const key of Object.keys(value)) {
          if (value[key] !== null && value[key] !== "" && typeof (value[key]) !== 'undefined') {
            let params = propName + '[' + key + ']';
            var subPart = encodeURIComponent(params) + "=";
            result += subPart + encodeURIComponent(value[key]) + "&";
          }
        }
      } else {
        result += part + encodeURIComponent(value) + "&";
      }
    }
  }
  return result
}

export const formatTime = (date) => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()
  return `${[year, month, day].map(formatNumber).join('-')}`
}

//判断时间是否超过n小时 time 比较时间 fiveHoursInMs：毫秒
export const isMoreThanHours = (time, fiveHoursInMs)=>{
  let formattedDate = time.replace(/-/g, '/'); 
  const diff = new Date().getTime() - new Date(formattedDate).getTime(); // 计算差值
  return diff < fiveHoursInMs
}

const formatNumber = (n) => {
  n = n.toString()
  return n[1] ? n : `0${n}`
}


